package it.traveldream.entities;


import it.traveldream.dto.HotelDTO;
import it.traveldream.dto.HotelDTO.HotelRoomType;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.CollectionTable;
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.JoinColumn;
import javax.persistence.MapKeyColumn;
import javax.persistence.MapKeyEnumerated;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

/**
 * Entity implementation class for Entity: Hotel
 *
 */
@Entity
@Table(name="Hotel")
@DiscriminatorValue(ProductType.HOTEL)

@NamedQueries({
@NamedQuery(
		name = "getAllHotelsInLocation",
		query = "SELECT h FROM Hotel h WHERE h.location = :location"
		),
@NamedQuery(
		name = "getAlternativeHotelInLocation",
		query = "SELECT h FROM Hotel h WHERE h.location = :location AND "
				+ "h.productId != :id"
		)
})
public class Hotel extends Product {

	private static final long serialVersionUID = 1L;
	
	@ElementCollection(targetClass = HotelRoom.class)
	@CollectionTable(name="HotelRooms", joinColumns= @JoinColumn(name="HOTELID"))
	@MapKeyColumn(name = "ROOMTYPE", length = 50, nullable = false)
	@MapKeyEnumerated(EnumType.STRING)
	@Column(name = "ROOM", length = 100)
	private Map<HotelRoomType, HotelRoom> hotelRooms;

	public Hotel() {
		super();
	}

	public Hotel(String name, Location location) {
		super();
		this.name = name;
		this.location = location;
	}

	public Map<HotelRoomType, HotelRoom> getHotelRooms() {
		return this.hotelRooms;
	}

	public void setHotelRooms(Map<HotelRoomType, HotelRoom> hotelRooms) {
		this.hotelRooms = hotelRooms;
	}

	@Override
	public HotelDTO toDTO(){
		HotelDTO dto = new HotelDTO();
		
		dto.setProductId(this.productId);
		dto.setName(this.name);
		dto.setLocation(this.location.toDTO());
		
		HashMap<HotelRoomType, Integer> rooms = new HashMap<>();
		HashMap<HotelRoomType, BigDecimal> prices = new HashMap<>();
		for (Map.Entry<HotelRoomType, HotelRoom> room: this.hotelRooms.entrySet()){
			rooms.put(room.getKey(), room.getValue().getRooms());
			prices.put(room.getKey(), room.getValue().getPrice());
		}
		dto.setRooms(rooms);
		dto.setPrices(prices);
		
		return dto;
	}
   
}
